package com.sangfor.ssl.service.setting;

import android.os.Environment;
import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.service.utils.xml.XmlReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/* loaded from: classes2.dex */
public class ResourceManager {
    public static final int FAVORITE_LIMIT = 64;
    public static final int HISTORY_LIMIT = 10;
    public static final String RC_SVC_RDESKTOP = "Terminal Service";
    public static final String RC_SVC_REMOTEAPP = "REMOTEAPP";
    public static final String RC_SVC_SHAREDESK = "SHAREDESK";
    public static final String RC_SVC_VIRTUALDESK = "VIRTUALDESK";
    public static final String RC_SVR_ALLIP = "vipall";
    public static final int RESOURCE_TPYE_L3VPN = 2;
    public static final int RESOURCE_TPYE_TCP = 1;
    public static final int RESOURCE_TPYE_WEB = 0;
    private static final String TAG = "ResourceManager";
    private static ResourceManager instance = null;
    public static boolean isAbleWork = true;
    private String mUserName;
    String mDnsWihteRule = "";
    Map<String, Object> mDnsRcMap = null;
    Map<String, Boolean> mSdRcIdMap = null;
    Map<String, Boolean> mFileLockRcIdList = null;
    private boolean mHasGlobalWebRc = false;
    private boolean mHaveL3vpnRc = false;
    private ArrayList<Integer> rappRcIds = null;
    ArrayList<Map<String, Object>> mGrpList = new ArrayList<>();
    Map<String, Map<String, Object>> mGrpMap = new HashMap();
    Map<String, ArrayList<Map<String, Object>>> mRcGrpMap = new HashMap();
    Map<String, Map<String, Object>> mRcMap = new HashMap();
    Map<String, Map<String, Object>> mEasyLinkMap = new HashMap();
    Map<String, Object> mOriginalData = new HashMap();
    private ArrayList<Map<String, Object>> mFavoriteList = new ArrayList<>();
    private ArrayList<Map<String, Object>> mHistoryList = new ArrayList<>();
    private Map<String, Map<String, Object>> mFavoriteMap = new HashMap();
    private Map<String, Map<String, Object>> mVisibleRcs = new HashMap();

    private ResourceManager() {
    }

    public void setmDnsWihteRule(String str) {
        this.mDnsWihteRule = str;
    }

    public static synchronized ResourceManager getInstance() {
        ResourceManager resourceManager;
        synchronized (ResourceManager.class) {
            if (instance == null) {
                instance = new ResourceManager();
            }
            resourceManager = instance;
        }
        return resourceManager;
    }

    public static synchronized void destoryObject() {
        synchronized (ResourceManager.class) {
            ResourceManager resourceManager = instance;
            if (resourceManager != null) {
                resourceManager.cleanData();
            }
        }
    }

    public void saveToSDCard(String str, String str2) {
        FileOutputStream fileOutputStream = null;
        try {
            try {
                try {
                    FileOutputStream fileOutputStream2 = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), str));
                    try {
                        fileOutputStream2.write(str2.getBytes());
                        fileOutputStream2.close();
                    } catch (Exception e) {
                        e = e;
                        fileOutputStream = fileOutputStream2;
                        e.printStackTrace();
                        fileOutputStream.close();
                    } catch (Throwable th) {
                        th = th;
                        fileOutputStream = fileOutputStream2;
                        try {
                            fileOutputStream.close();
                        } catch (IOException e2) {
                            e2.printStackTrace();
                        }
                        throw th;
                    }
                } catch (Exception e3) {
                    e = e3;
                }
            } catch (Throwable th2) {
                th = th2;
            }
        } catch (IOException e4) {
            e4.printStackTrace();
        }
    }

    public boolean parseRcXml(String str) {
        if (str == null || str.length() <= 0) {
            return false;
        }
        synchronized (this) {
            cleanData();
            Map<String, Object> parse = XmlReader.parse(str);
            this.mOriginalData = parse;
            if (parse == null) {
                this.mOriginalData = new HashMap();
                return false;
            }
            this.mOriginalData = (Map) parse.get("Resource");
            this.mUserName = SettingManager.getInstance().getString(SettingManager.SETTING_USERNAME, null);
            handleRc();
            handleGrp();
            handleEasyLink();
            return true;
        }
    }

    public Map<String, Object> getGroupByID(String str) {
        if (str == null) {
            return null;
        }
        return this.mGrpMap.get(str);
    }

    public ArrayList<Map<String, Object>> getGroups() {
        return this.mGrpList;
    }

    public Map<String, Object> getEasyLink(String str) {
        return this.mEasyLinkMap.get(str);
    }

    public ArrayList<Map<String, Object>> getRcsByGroupID(String str) {
        Map<String, ArrayList<Map<String, Object>>> map;
        if (str == null || (map = this.mRcGrpMap) == null) {
            return new ArrayList<>();
        }
        return map.get(str);
    }

    public Map<String, Object> getRcByID(String str) {
        Map<String, Map<String, Object>> map;
        if (str == null || (map = this.mRcMap) == null) {
            return null;
        }
        return map.get(str);
    }

    public Map<String, String> getDnsInfo() {
        Object obj;
        Map<String, Object> map = this.mOriginalData;
        if (map == null || (obj = map.get("Dns")) == null || !(obj instanceof Map)) {
            return null;
        }
        return (Map) obj;
    }

    public Map<String, Object> getDefaultWebRc() {
        Object obj;
        Map<String, Object> rcByID;
        Map<String, Object> map = this.mOriginalData;
        if (map != null && (obj = map.get("Other")) != null && !(obj instanceof String)) {
            String str = (String) ((Map) obj).get("defaultRcId");
            if (Integer.parseInt(str) > 0 && (rcByID = getRcByID(str)) != null && Integer.parseInt((String) rcByID.get("type")) == 0) {
                return rcByID;
            }
        }
        return null;
    }

    public ArrayList<Map<String, Object>> getResouceByType(int i) {
        ArrayList<Map<String, Object>> arrayList = new ArrayList<>();
        for (String str : this.mRcMap.keySet()) {
            Map<String, Object> map = this.mRcMap.get(str);
            if (Integer.parseInt((String) map.get("type")) == i) {
                arrayList.add(map);
            }
        }
        return arrayList;
    }

    public boolean containGlobalWebRc() {
        return this.mHasGlobalWebRc;
    }

    public boolean containL3vpnRc() {
        return this.mHaveL3vpnRc;
    }

    public ArrayList<Map<String, Object>> getFavorites() {
        return this.mFavoriteList;
    }

    public ArrayList<Map<String, Object>> getHistory() {
        return this.mHistoryList;
    }

    public boolean isFavorite(Map<String, Object> map) {
        return this.mFavoriteMap.containsKey(map.get("id"));
    }

    private void cleanListMapData(ArrayList<Map<String, Object>> arrayList) {
        if (arrayList == null) {
            return;
        }
        arrayList.clear();
    }

    private void cleanMapListData(Map<String, ArrayList<Map<String, Object>>> map) {
        if (map == null) {
            return;
        }
        map.clear();
    }

    private void cleanMapMapData(Map<String, Map<String, Object>> map) {
        if (map == null) {
            return;
        }
        map.clear();
    }

    private void cleanData() {
        this.mHaveL3vpnRc = false;
        Map<String, Object> map = this.mOriginalData;
        if (map != null) {
            map.clear();
        }
        cleanMapListData(this.mRcGrpMap);
        cleanMapMapData(this.mGrpMap);
        cleanMapMapData(this.mRcMap);
        cleanMapMapData(this.mEasyLinkMap);
        cleanMapMapData(this.mFavoriteMap);
        cleanMapMapData(this.mVisibleRcs);
        cleanListMapData(this.mGrpList);
        cleanListMapData(this.mFavoriteList);
        cleanListMapData(this.mHistoryList);
    }

    private void handleRc() {
        Object obj;
        if (this.mOriginalData == null) {
            return;
        }
        Log.error(TAG, "handleRc");
        Object obj2 = this.mOriginalData.get("Rcs");
        if (obj2 == null || (obj2 instanceof String) || (obj = ((Map) obj2).get("Rc")) == null) {
            return;
        }
        ArrayList arrayList = new ArrayList();
        if (obj instanceof ArrayList) {
            arrayList = (ArrayList) obj;
        } else if (obj instanceof Map) {
            arrayList.add((Map) obj);
        }
        Map<String, Object> dNSRcIpMap = getDNSRcIpMap();
        for (int i = 0; i < arrayList.size(); i++) {
            Map<String, Object> map = (Map) arrayList.get(i);
            ResourceHandler.handle(map, dNSRcIpMap);
            String str = (String) map.get("id");
            this.mRcMap.put(str, map);
            if (ResourceHandler.isAllWebRc(map)) {
                this.mHasGlobalWebRc = true;
            } else if (!filterRc(map)) {
                String str2 = (String) map.get("rc_grp_id");
                ArrayList<Map<String, Object>> arrayList2 = this.mRcGrpMap.get(str2);
                if (arrayList2 == null) {
                    arrayList2 = new ArrayList<>();
                    this.mRcGrpMap.put(str2, arrayList2);
                }
                arrayList2.add(map);
                this.mVisibleRcs.put(str, map);
            }
        }
    }

    private void handleGrp() {
        if (this.mOriginalData == null) {
            return;
        }
        this.mGrpList.clear();
        Object obj = this.mOriginalData.get("RcGroups");
        if (obj == null || (obj instanceof String)) {
            return;
        }
        ArrayList arrayList = new ArrayList();
        Object obj2 = ((Map) obj).get("Group");
        if (obj2 != null) {
            if (obj2 instanceof ArrayList) {
                arrayList = (ArrayList) obj2;
            } else if (obj2 instanceof Map) {
                arrayList.add((Map) obj2);
            }
            for (int i = 0; i < arrayList.size(); i++) {
                Map<String, Object> map = (Map) arrayList.get(i);
                String str = (String) map.get("id");
                this.mGrpMap.put(str, map);
                if (this.mRcGrpMap.get(str) != null) {
                    this.mGrpList.add(map);
                }
            }
        }
    }

    private void handleEasyLink() {
        Object obj;
        boolean z;
        Object obj2;
        Map<String, Object> map = this.mOriginalData;
        if (map == null || (obj = map.get("Easylink")) == null || ((z = obj instanceof String)) || (obj2 = ((Map) obj).get("ElnkRc")) == null || z) {
            return;
        }
        ArrayList arrayList = new ArrayList();
        if (obj2 instanceof ArrayList) {
            arrayList = (ArrayList) obj2;
        } else if (obj2 instanceof Map) {
            arrayList.add((Map) obj2);
        }
        Iterator it = arrayList.iterator();
        while (it.hasNext()) {
            Map<String, Object> map2 = (Map) it.next();
            this.mEasyLinkMap.put((String) map2.get("Id"), map2);
        }
    }

    private boolean filterRc(Map<String, Object> map) {
        Map<String, Boolean> sDRcList = getSDRcList();
        Map<String, Boolean> fileLockRcList = getFileLockRcList();
        int parseInt = Integer.parseInt((String) map.get("type"));
        int parseInt2 = Integer.parseInt((String) map.get("attr"));
        String lowerCase = map.get("svc").toString().toLowerCase();
        String str = (String) map.get("id");
        SystemConfiguration systemConfiguration = SystemConfiguration.getInstance();
        boolean isL3vpnServiceSupported = systemConfiguration.isL3vpnServiceSupported();
        if ((parseInt2 & 12) != 0) {
            return true;
        }
        if ((parseInt2 & 1) != 0) {
            if (isL3vpnServiceSupported && 2 == parseInt) {
                this.mHaveL3vpnRc = true;
            }
            return true;
        } else if (!lowerCase.equals(RC_SVR_ALLIP) || isL3vpnServiceSupported) {
            if (sDRcList == null || sDRcList.get(str) == null) {
                if (fileLockRcList == null || fileLockRcList.get(str) == null) {
                    if (parseInt == 0) {
                        if (lowerCase.equals("fileshare")) {
                            return true;
                        }
                        if (!lowerCase.equals("http") && !lowerCase.equals("https") && map.get("enable_disguise").toString().equals(SystemConfiguration.ON)) {
                            return true;
                        }
                    }
                    if (2 == parseInt) {
                        if (isL3vpnServiceSupported) {
                            this.mHaveL3vpnRc = true;
                            return false;
                        }
                        String str2 = (String) systemConfiguration.get(3, "Other.pptp_grpolicy");
                        if (str2 == null || !str2.equals(SystemConfiguration.ON)) {
                            return true;
                        }
                    }
                    return (1 == parseInt && !isTcpTerminalTypeRc(lowerCase)) || map.get("ips") == null;
                }
                return true;
            }
            return true;
        } else {
            return true;
        }
    }

    public Map<String, Object> getDNSRcIpMap() {
        String[] split;
        Map<String, Object> map = this.mDnsRcMap;
        if (map == null) {
            this.mDnsRcMap = new HashMap();
        } else {
            map.clear();
        }
        Map map2 = (Map) this.mOriginalData.get("Dns");
        if (map2 == null) {
            return this.mDnsRcMap;
        }
        String str = (String) map2.get("data");
        if (str == null || str.trim().equals("")) {
            return this.mDnsRcMap;
        }
        for (String str2 : str.split(";")) {
            if (!str2.trim().equals("")) {
                String[] split2 = str2.split(":");
                if (split2.length == 3) {
                    this.mDnsRcMap.put(split2[1], split2[2]);
                }
            }
        }
        return this.mDnsRcMap;
    }

    private Map<String, Boolean> getSDRcList() {
        Map<String, Boolean> map = this.mSdRcIdMap;
        if (map == null) {
            this.mSdRcIdMap = new HashMap();
        } else {
            map.clear();
        }
        Map map2 = (Map) this.mOriginalData.get("SD");
        if (map2 == null) {
            return this.mSdRcIdMap;
        }
        Map map3 = (Map) map2.get("Global");
        if (map3 == null) {
            return this.mSdRcIdMap;
        }
        if (!((String) map3.get("enable")).equals(SystemConfiguration.ON)) {
            return this.mSdRcIdMap;
        }
        Map map4 = (Map) map2.get("rclist");
        if (map4 == null) {
            return this.mSdRcIdMap;
        }
        String str = (String) map4.get("rclist");
        if (str.trim().equals("")) {
            return this.mSdRcIdMap;
        }
        for (String str2 : str.split(",")) {
            this.mSdRcIdMap.put(str2, true);
        }
        return this.mSdRcIdMap;
    }

    private Map<String, Boolean> getFileLockRcList() {
        String[] split;
        Map<String, Boolean> map = this.mFileLockRcIdList;
        if (map == null) {
            this.mFileLockRcIdList = new HashMap();
        } else {
            map.clear();
        }
        Map map2 = (Map) this.mOriginalData.get("FileLock");
        if (map2 == null) {
            return this.mFileLockRcIdList;
        }
        String str = (String) map2.get("data");
        if (str.trim().equals("")) {
            return this.mFileLockRcIdList;
        }
        for (String str2 : str.split(";")) {
            if (!str2.trim().equals("")) {
                this.mFileLockRcIdList.put(str2.split(":")[0], true);
            }
        }
        return this.mFileLockRcIdList;
    }

    public ArrayList<Integer> getAllRappRcIds() {
        if (this.mRcGrpMap == null) {
            return null;
        }
        ArrayList<Integer> arrayList = this.rappRcIds;
        if (arrayList == null) {
            this.rappRcIds = new ArrayList<>();
        } else {
            arrayList.clear();
        }
        for (Map.Entry<String, ArrayList<Map<String, Object>>> entry : this.mRcGrpMap.entrySet()) {
            ArrayList<Map<String, Object>> value = entry.getValue();
            for (int i = 0; i < value.size(); i++) {
                Map<String, Object> map = value.get(i);
                String lowerCase = map.get("svc").toString().toLowerCase();
                int parseInt = Integer.parseInt((String) map.get("type"));
                boolean equals = lowerCase.equals(RC_SVC_REMOTEAPP.toLowerCase());
                boolean endsWith = lowerCase.endsWith(RC_SVC_SHAREDESK.toLowerCase());
                if (1 == parseInt && (equals || endsWith)) {
                    this.rappRcIds.add(Integer.valueOf(Integer.parseInt((String) map.get("id"))));
                }
            }
        }
        return this.rappRcIds;
    }

    /* loaded from: classes2.dex */
    public static class ResourceHandler {
        public static String htmlDecode(String str) {
            return str == null ? "" : str.replaceAll("&quot;", "\"").replaceAll("&quos;", "'").replaceAll("&lt;", "<").replaceAll("&gt;", ">").replaceAll("&amp;", "&");
        }

        /* JADX WARN: Code restructure failed: missing block: B:41:0x00e7, code lost:
            if (r9 == null) goto L27;
         */
        /*
            Code decompiled incorrectly, please refer to instructions dump.
            To view partially-correct code enable 'Show inconsistent code' option in preferences
        */
        public static java.util.Map<java.lang.String, java.lang.Object> handle(java.util.Map<java.lang.String, java.lang.Object> r18, java.util.Map<java.lang.String, java.lang.Object> r19) {
            /*
                Method dump skipped, instructions count: 308
                To view this dump change 'Code comments level' option to 'DEBUG'
            */
            throw new UnsupportedOperationException("Method not decompiled: com.sangfor.ssl.service.setting.ResourceManager.ResourceHandler.handle(java.util.Map, java.util.Map):java.util.Map");
        }

        public static String getAddrByName(String str, Map<String, Object> map) {
            String str2;
            String str3;
            int indexOf;
            int indexOf2 = str.indexOf("://");
            if (indexOf2 != -1) {
                str2 = str.substring(indexOf2 + 3);
                int indexOf3 = str2.indexOf(":");
                if (indexOf3 != -1) {
                    str2 = str2.substring(0, indexOf3);
                }
                int indexOf4 = str2.indexOf("/");
                if (indexOf4 != -1) {
                    str2 = str2.substring(0, indexOf4);
                }
            } else {
                String str4 = str.split("[:/]")[0];
                if (str4.matches("[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}")) {
                    return str4;
                }
                str2 = str;
            }
            int indexOf5 = str2.indexOf("\\\\");
            if (indexOf5 != -1 && (indexOf = (str2 = str2.substring(indexOf5 + 2)).indexOf("\\")) != -1) {
                str2 = str2.substring(0, indexOf);
            }
            if (map == null || map.get(str2) == null) {
                str3 = null;
            } else {
                String str5 = str2;
                str2 = map.get(str2).toString();
                str3 = str5;
            }
            if (str2.matches("(([0-9]{1,3})(\\.[0-9]{1,3}){3})")) {
                return str3 != null ? str.replace(str3, str2) : str;
            }
            return null;
        }

        public static boolean isAllWebRc(Map<String, Object> map) {
            if (map == null) {
                return false;
            }
            return ((String) map.get("id")).equals("-99") && ((String) map.get("authorization")).equals(SystemConfiguration.ON);
        }
    }

    public boolean isTcpTerminalTypeRc(String str) {
        if (TextUtils.isEmpty(str)) {
            return false;
        }
        return str.equals(RC_SVC_RDESKTOP.toLowerCase()) || str.equals(RC_SVC_REMOTEAPP.toLowerCase()) || str.equals(RC_SVC_SHAREDESK.toLowerCase()) || str.endsWith(RC_SVC_VIRTUALDESK.toLowerCase());
    }
}
